// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2005-5-8

#ifndef __CHECKPE_H_COMMON_GUARD__
#define __CHECKPE_H_COMMON_GUARD__

#pragma once

namespace JoyGuard
{

//##ModelId=44C4B60E0119
class CheckPe
{
public:
	//##ModelId=44C4B60E01A5
    enum ErrorCode
    {
		//##ModelId=44C4B60E01A7
        EC_OK,
		//##ModelId=44C4B60E01A8
        EC_FAILED,
		//##ModelId=44C4B60E01B5
        EC_READ_MEMORY,
		//##ModelId=44C4B60E01B6
        EC_CHECK_IMAGE,
		//##ModelId=44C4B60E01B7
        EC_CHECK_FILE,
		//##ModelId=44C4B60E01C5
        EC_NO_MODULE,
    };
	//##ModelId=44C4B60E01C6
    class ECheckStop : public std::exception
    {
    };

    // process data
	//##ModelId=44C4B60E011A
    HANDLE  m_process;
	//##ModelId=44C4B60E0128
    DWORD   m_processId;
	//##ModelId=44C4B60E0129
    BYTE*   m_module;
	//##ModelId=44C4B60E012A
    DWORD   m_checkRva;
	//##ModelId=44C4B60E012B
    DWORD   m_checkSize;
	//##ModelId=44C4B60E0138
    DWORD   m_imageSize;

    // pe file data
	//##ModelId=44C4B60E0139
    DWORD   m_fileHash;

    // check data
	//##ModelId=44C4B60E013B
    std::vector<BYTE> m_baseData;
	//##ModelId=44C4B60E014B
    std::vector<BYTE> m_tempData;

    // check control
	//##ModelId=44C4B60E014F
    DWORD   m_error;
	//##ModelId=44C4B60E0157
    HANDLE  m_waitEvent;
	//##ModelId=44C4B60E0158
    DWORD   m_readSize;
	//##ModelId=44C4B60E0159
    DWORD   m_readDelay;
	//##ModelId=44C4B60E0167
    DWORD   m_readed;
	//##ModelId=44C4B60E0168
    bool    m_stopped;

	//##ModelId=44C4B60E0169
    void CheckStop();
	//##ModelId=44C4B60E016A
    void ReadMemory(BYTE* dst, BYTE* src, int len);
public:
	//##ModelId=44C4B60E017A
    CheckPe();

	//##ModelId=44C4B60E017B
    static bool ReadCodeData(const char* fileName, std::vector<BYTE>& codeData, DWORD& codeRva, DWORD& codeSize, DWORD& imageSize);

	//##ModelId=44C4B60E018A
    bool CreateByFile(const char* fileName, DWORD hash = 0);
	//##ModelId=44C4B60E0197
    bool CreateByModule(const char* modName);
	//##ModelId=44C4B60E0199
    bool Create();
	//##ModelId=44C4B60E019A
    bool Check();
};

} // namespace JoyGuard

#endif  // __CHECKPE_H_COMMON_GUARD__
